package com.jiangkai.common.beetl.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.beetl.core.GeneralVarTagBinding;

import com.baomidou.mybatisplus.plugins.Page;

public class PagenateTag extends GeneralVarTagBinding{

	private Logger logger = Logger.getLogger(PagenateTag.class);

	private int currPage; // 当前页
	private int firstPage; // 首页
	private int lastPage; // 尾页
	private int prePage; // 上一页
	private int nextPage; // 下一页
	private String funcName; // 要执行的JS函数名
	private List<Integer> pageNums; // 页码

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void render() {
		funcName = getAttributeValue("func").toString();
		Page pagenate = (Page) getAttributeValue("page");

		StringBuilder html = new StringBuilder("");
		if (funcName != null && !funcName.equals("") && pagenate != null) {
			currPage = pagenate.getCurrent();
			firstPage = 1;
			lastPage = (int) pagenate.getPages();
			prePage = currPage <= 1 ? 1 : currPage - 1;
			nextPage = currPage >= lastPage ? lastPage : lastPage + 1;
			
			// 页码，默认显示9页
			int showPgs = 5;
			int midPgNo = showPgs / 2 + 1;
			int startNo = 0;
			int endNo = 0;
			if (lastPage <= midPgNo || lastPage <= showPgs) {
				startNo = 1;
				endNo = lastPage;
			} else if (currPage <= midPgNo) {
				startNo = 1;
				endNo = showPgs;
			} else if (currPage > midPgNo) {
				if (currPage + (midPgNo - 1) <= lastPage) {
					startNo = currPage - (midPgNo - 1);
					endNo = currPage + (midPgNo - 1);
				} else {
					startNo = lastPage - showPgs + 1;
					endNo = lastPage;
				}
			}
			 pageNums = new ArrayList<Integer>();
			for (int i = startNo; i <= endNo; i++) {
				pageNums.add(i);
			}

			String beforeDisable = currPage==1?"disabled":"";
			String afterDisable = currPage==lastPage?"disabled":"";
			html.append("<div class='pagination justify-content-center'>");
			html.append("<span class='pageinfo form-inline'>当前第 <b>"+currPage+"</b>/"+lastPage+" 页，共"+pagenate.getTotal()+"条数据</span>");
			
			if (firstPage < lastPage) {
				// 只有大于一页的时候才显示分页标签
				// 只有当前页码大于首页页码时才显示“上一页”
//				if (currPage > firstPage) {
					html.append("<li class='page-item "+beforeDisable+"'><a class='page-link' href=\"javascript:" + funcName + "(1)"
							+ "\" tabindex='-1'><i class='material-icons'>first_page</i></a></li>");
					html.append("<li class='page-item "+beforeDisable+"'><a class='page-link' href=\"javascript:" + funcName + "("+(currPage-1)+")"
							+ "\" tabindex='-1'><i class='material-icons'>chevron_left</i></a></li>");
//				}

				// 组织页码
				for (Integer pn : pageNums) {
					if (pn == currPage) {
						html.append("<li class='page-item active'><a class='page-link'>" + pn + "</a></li>");
					} else {
						html.append(
								"<li class='page-item'><a class='page-link' href=\"javascript:" + funcName + "('" + pn + "')" + "\">" + pn + "</a></li>");
					}
				}

				// 只有当前页码小于尾页页码时才显示“下一页”
//				if (currPage < lastPage) {
					html.append("<li class='page-item "+afterDisable+"'><a class='page-link' href=\"javascript:" + funcName + "("+(currPage+1)+")"
							+ "\" tabindex='-1'><i class='material-icons'>chevron_right</i></a></li>");
					html.append("<li class='page-item "+afterDisable+"'><a class='page-link' href=\"javascript:" + funcName + "("+lastPage+")"
							+ "\" tabindex='-1'><i class='material-icons'>last_page</i></a></li>");
//				}
			}
			html.append("</div>");
		} else {
			html.append(".");//相关参数未定义
		}
		
		
		try {
			ctx.byteWriter.writeString(html.toString());
		} catch (IOException e) {
			logger.error("自定义标签-分页出错", e);
		}
	}
}
